package cn.cuiliangpeng.org.controller;

import cn.cuiliangpeng.basic.utils.ExcelUtil;
import cn.cuiliangpeng.org.domain.ShopAuditLog;
import cn.cuiliangpeng.org.domain.vo.ShopEcharts;
import cn.cuiliangpeng.org.service.IShopService;
import cn.cuiliangpeng.org.domain.Shop;
import cn.cuiliangpeng.org.query.ShopQuery;
import cn.cuiliangpeng.basic.utils.AjaxResult;
import cn.cuiliangpeng.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop" )
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult updateAndAdd(@RequestBody Shop shop) {
        if ( shop.getId() != null){
                shopService.update(shop);
        } else{
                shopService.save(shop);
        }
        return AjaxResult.me();
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult delete(@PathVariable("id" ) Long id) {
            shopService.delete(id);
        return AjaxResult.me();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patch(@RequestBody Long[] ids) {
            shopService.patch(ids);
        return AjaxResult.me();
    }
    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}" )
    public Shop findById(@PathVariable("id" ) Long id) {
        return shopService.findById(id);
    }

    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid Shop shop){
        shopService.settlement(shop);
        return AjaxResult.me();
    }

    /**
     * 审核通过
     *
     * @param log
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log) throws MessagingException {
        shopService.pass(log);
        return AjaxResult.me();
    }

    /**
     * 店铺驳回
     * @param log
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log) throws MessagingException {
        shopService.reject(log);
        return AjaxResult.me();
    }

    /**
     * Excel导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shopList = shopService.findAll();
        ExcelUtil.exportExcel(shopList,null,"店铺",Shop.class,"店铺信息.xlsx",response);
    }

    /**
     * Excel导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : shops) {
            shopService.save(shop);
        }
    }

    /**
     * 图形显示
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopEcharts> echarts(){
        return shopService.echarts();
    }

    /**
     * 店铺激活业务
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    private AjaxResult active(@PathVariable("id") Long id ){
        shopService.active(id);
        return AjaxResult.me();
    }
}
